import time
import allure
from selenium import webdriver
from UI_test.page_base.Base_page import BasePage


class LoginPage(BasePage):
    username_input = '//input[@placeholder="请输入账号"]'  # 请输入账号
    password_input = '//input[@placeholder="请输入密码"]'  # 请输入密码
    login_button = '//button'  # 登录按钮
    username_txt = "el-form-item__error"  # 获取请输入账号提示文本
    alert_txt = '//div[@role="alert"]/p'  # 获取提示框文本

    @allure.step('设置全局等待时间为n秒')
    def set_implicitly_wait(self, n):
        self.driver.implicitly_wait(n)
        return self

    @allure.step('刷新网站页面')
    def refresh_page(self):
        self.driver.refresh()
        return self

    @allure.step('判断是否进入网站登录界面,显示是否正常')
    def is_login_page(self):
        return self.by_xpath(self.username_input).get_attribute('placeholder')

    @allure.step('点击登录按钮')
    def click_login_button(self):
        self.by_xpath(self.login_button).click()
        return self

    @allure.step('清空用户名输入框')
    def clear_username(self):
        self.by_xpath(self.username_input).clear()
        return self

    @allure.step('输入用户名')
    def input_username(self, username):
        self.by_xpath(self.username_input).send_keys(username)
        return self

    @allure.step('输入用户密码')
    def input_password(self, password):
        self.by_xpath(self.password_input).send_keys(password)
        return self

    @allure.step('获取请输入账号和用户名的提示文本')
    def get_user_tips(self):
        list_txt = []
        elements = self.by_class_names(self.username_txt)
        for element in elements:
            list_txt.append(element.text)
        return list_txt

    @allure.step('获取提示框里面的文本')
    def get_alert_text(self):
        return self.by_xpath(self.alert_txt).text
